﻿using System;
using System.Drawing;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.BLL;
using XiaoGang.Soft.XGWMS.COM;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class M007_GOODS : FormBase
    {
        #region 变量定义

        private bool blFormLoad = false;

        /// <summary>
        /// 是否是选择商品
        /// </summary>
        private bool IsSelect = false;
        private string FatherForm = "";
        private readonly MODEL.MOD_M007_GOODS SearchModel;
        private readonly MODEL.MOD_M007_GOODS entityGoods;
        private readonly BLL.BLL_M007_GOODS bll_M007_GOODS;
        private int intFirstDisplayedScrollingRowIndex = 0;
        private bool AddRowFlag = true;
        private bool SearchFlag = false;

        /// <summary>
        /// 商品
        /// </summary>
        private const string dataName = "商品";
        private bool isInit = true;

        #endregion

        #region 方法

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="isSelectGoods">是否为选择商品时创建</param>
        public M007_GOODS(bool isSelectGoods)
        {
            InitializeComponent();
            bll_M007_GOODS = new BLL_M007_GOODS();
            SearchModel = new MODEL.MOD_M007_GOODS();
            entityGoods = new MODEL.MOD_M007_GOODS();
            IsSelect = isSelectGoods;
            InitialProcess();
        }

        /// <summary>
        /// 窗口初始化
        /// </summary>
        protected void InitialProcess()
        {
            SetTabIndex();
            SetEnable();
            InitialComBox();
            InitialGridView();
            ClearControl();
            isInit = false;
        }

        /// <summary>
        /// 设置Tab顺
        /// </summary>
        protected void SetTabIndex()
        {
            int intIndex = 0;
            txtGOODS_NM1.TabIndex = intIndex; intIndex++;
            txtPROVIDER.TabIndex = intIndex; intIndex++;
            txtMEMO.TabIndex = intIndex; intIndex++;
            cmbGOODS_GROUP.TabIndex = intIndex; intIndex++;
            cmbSTOREROOM_ID.TabIndex = intIndex; intIndex++;
            btnAdd.TabIndex = intIndex; intIndex++;
            btnExit.TabIndex = intIndex; intIndex++;
            txtGOODS_ID.TabIndex = intIndex; intIndex++;
            txtGOODS_NO.TabIndex = intIndex; intIndex++;
        }

        /// <summary>
        /// 设置空间可用性
        /// </summary>
        protected void SetEnable()
        {
            grpGoodsInfo.Enabled = true;
            txtGOODS_ID.Enabled = true;
            txtGOODS_NO.Enabled = true;
            txtGOODS_NM1.Enabled = true;
            txtPROVIDER.Enabled = true;
            txtMEMO.Enabled = true;
            cmbGOODS_GROUP.Enabled = true;
            cmbSTOREROOM_ID.Enabled = true;//仓库
            grpSearchResult.Enabled = true;
            grdMain.Enabled = true;
            btnAdd.Enabled = true;
            btnExit.Enabled = true;
        }

        /// <summary>
        /// 初始化下拉列表框
        /// </summary>
        protected void InitialComBox()
        {
            Method.CmbDataBound("GOODS_GROUP_ID", cmbGOODS_GROUP);
            cmbGOODS_GROUP.SelectedIndex = 0;
            Method.CmbDataBound("STOREROOM_ID", cmbSTOREROOM_ID);
            cmbSTOREROOM_ID.SelectedIndex = 0;
        }

        /// <summary>
        /// 初始化GridView
        /// </summary>
        protected void InitialGridView()
        {
            grdMain.AllowUserToAddRows = false;
            grdMain.AllowUserToResizeColumns = true;
            grdMain.AllowUserToDeleteRows = false;
            grdMain.AllowUserToResizeRows = false;
            grdMain.ColumnHeadersHeight = 30;
            grdMain.ColumnHeadersVisible = true;
            grdMain.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            grdMain.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdMain.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
            grdMain.ReadOnly = true;
            grdMain.RowHeadersVisible = false;
            grdMain.RowTemplate.Resizable = DataGridViewTriState.False;
            grdMain.RowTemplate.Height = 25;
            grdMain.RowTemplate.ReadOnly = true;
            grdMain.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            grdMain.ScrollBars = ScrollBars.Both;
            grdMain.TabStop = false;

            grdMain.Columns.Add("NO.", "NO.");
            grdMain.Columns.Add("GOODS_ID", "商品编号");
            grdMain.Columns.Add("GOODS_NO", "商品条码");
            grdMain.Columns.Add("GOODS_GROUP_NM", "分类名称");
            grdMain.Columns.Add("GOODS_GROUP", "商品分类编号");
            grdMain.Columns.Add("GOODS_NM1", "商品名称");
            grdMain.Columns.Add("GOODS_NM2", "商品名称2");
            grdMain.Columns.Add("MONEY", "单价");
            grdMain.Columns.Add("Discount", "折扣");
            grdMain.Columns.Add("Units", "数量");
            grdMain.Columns["Units"].Frozen = true;
            DataGridViewButtonColumn btnDelete = new DataGridViewButtonColumn() { Text = "删除", Width = 80, HeaderText = "删除", SortMode = DataGridViewColumnSortMode.NotSortable, Resizable = DataGridViewTriState.False, Frozen = true };
            grdMain.Columns.Add(btnDelete);
            grdMain.Columns.Add("PROVIDER", "供应商");
            grdMain.Columns.Add("MAX_WARN", "最大库存");
            grdMain.Columns.Add("MIN_WARN", "最小库存");
            grdMain.Columns.Add("MEMO", "备注");
            grdMain.Columns.Add("TRANS_STATE", "状态");

            grdMain.Columns["NO."].Width = 50;
            grdMain.Columns["NO."].Frozen = true;
            grdMain.Columns["NO."].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdMain.Columns["NO."].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdMain.Columns["NO."].Resizable = DataGridViewTriState.False;
            grdMain.Columns["GOODS_ID"].Width = 150;
            //grdmentGoodsInfo.Columns["GOODS_ID"].Visible = false;
            grdMain.Columns["GOODS_ID"].Resizable = DataGridViewTriState.False;
            grdMain.Columns["GOODS_ID"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdMain.Columns["GOODS_NO"].Width = 150;
            grdMain.Columns["GOODS_NO"].Frozen = true;
            //grdmentGoodsInfo.Columns["GOODS_NO"].Visible = false;
            grdMain.Columns["GOODS_NO"].Resizable = DataGridViewTriState.False;
            grdMain.Columns["GOODS_NO"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdMain.Columns["GOODS_GROUP_NM"].Width = 120;
            grdMain.Columns["GOODS_GROUP"].Width = 0;
            grdMain.Columns["GOODS_GROUP"].Visible = false;
            grdMain.Columns["GOODS_GROUP"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdMain.Columns["GOODS_NM1"].Width = 200;
            grdMain.Columns["GOODS_NM1"].Frozen = true;
            grdMain.Columns["GOODS_NM2"].Width = 0;
            grdMain.Columns["GOODS_NM2"].Visible = false;
            grdMain.Columns["GOODS_NM2"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdMain.Columns["PROVIDER"].Width = 120;
            grdMain.Columns["PROVIDER"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdMain.Columns["MONEY"].Width = 80;
            grdMain.Columns["MONEY"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            //grdMain.Columns["MONEY"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdMain.Columns["Discount"].Width = 80;
            grdMain.Columns["Discount"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdMain.Columns["Discount"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdMain.Columns["Units"].Width = 80;
            grdMain.Columns["Units"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            //grdMain.Columns["Units"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdMain.Columns["MAX_WARN"].Width = 80;
            grdMain.Columns["MAX_WARN"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdMain.Columns["MAX_WARN"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdMain.Columns["MIN_WARN"].Width = 80;
            grdMain.Columns["MIN_WARN"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdMain.Columns["MIN_WARN"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdMain.Columns["MEMO"].Width = 200;
            grdMain.Columns["MEMO"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdMain.Columns["TRANS_STATE"].Width = 0;
            grdMain.Columns["TRANS_STATE"].Visible = false;
        }

        /// <summary>
        /// 清除控件值
        /// </summary>
        protected void ClearControl()
        {
            txtGOODS_ID.Text = "";
            txtGOODS_ID.Tag = "";
            txtGOODS_NO.Text = "";
            txtGOODS_NM1.Text = "";
            txtPROVIDER.Text = "";
            txtMEMO.Text = "";
            cmbSTOREROOM_ID.SelectedIndex = 0;//仓库
            ckbDelete.Checked = false;
            ckbNomal.Checked = true;
            cmbGOODS_GROUP.SelectedIndex = 0;
        }

        /// <summary>
        /// 查询并显示数据
        /// </summary>
        private void BindGridData()
        {
            if (isInit)
            {
                return;
            }
            grdMain.Rows.Clear();
            SearchModel.GOODS_ID = txtGOODS_ID.Text.Trim();
            SearchModel.GOODS_NO = txtGOODS_NO.Text.Trim();
            SearchModel.GOODS_NM1 = txtGOODS_NM1.Text.Trim();
            if (cmbGOODS_GROUP.SelectedIndex != -1)
            {
                SearchModel.GOODS_GROUP = cmbGOODS_GROUP.SelectedValue.ToString().Trim();
            }
            else
            {
                SearchModel.GOODS_GROUP = "";
            }
            SearchModel.PROVIDER = txtPROVIDER.Text.Trim();
            
            SearchModel.MEMO = txtMEMO.Text.Trim();

            if (ckbNomal.Checked && ckbDelete.Checked)
            {
                SearchModel.SearchType = 0;//全检索
            }
            else if (!ckbNomal.Checked && !ckbDelete.Checked)
            {
                SearchModel.SearchType = 0;//全检索
            }
            else if (!ckbNomal.Checked && ckbDelete.Checked)
            {
                SearchModel.SearchType = 2;//删除
            }
            else if (ckbNomal.Checked && !ckbDelete.Checked)
            {
                SearchModel.SearchType = 1;//正常
            }
            ReturnValue rvGoods = bll_M007_GOODS.SearchProcess(SearchModel);
            if (rvGoods.IsSuccess == false)
            {
                Method.MessageShow(rvGoods.ErrorID, dataName);
                return;
            }
            for (int i = 0; i < rvGoods.ResultDataSet.Tables[0].Rows.Count; i++)
            {
                grdMain.Rows.Add((i + 1).ToString(),
                    rvGoods.ResultDataSet.Tables[0].Rows[i]["GOODS_ID"].ToString().Trim(),
                    rvGoods.ResultDataSet.Tables[0].Rows[i]["GOODS_NO"].ToString().Trim(),
                    rvGoods.ResultDataSet.Tables[0].Rows[i]["GOODS_GROUP_NM"].ToString().Trim(),
                    rvGoods.ResultDataSet.Tables[0].Rows[i]["GOODS_GROUP"].ToString().Trim(),
                    rvGoods.ResultDataSet.Tables[0].Rows[i]["GOODS_NM1"].ToString().Trim(),
                    rvGoods.ResultDataSet.Tables[0].Rows[i]["GOODS_NM2"].ToString().Trim(),
                    rvGoods.ResultDataSet.Tables[0].Rows[i]["MONEY"].ToString().Trim() == "" ? null : rvGoods.ResultDataSet.Tables[0].Rows[i]["MONEY"],//单价
                    string.IsNullOrEmpty(rvGoods.ResultDataSet.Tables[0].Rows[i]["Discount"].ToString().Trim()) ? null : rvGoods.ResultDataSet.Tables[0].Rows[i]["Discount"],//折扣
                    rvGoods.ResultDataSet.Tables[0].Rows[i]["Units"],//数量
                    "删除",
                    rvGoods.ResultDataSet.Tables[0].Rows[i]["PROVIDER"].ToString().Trim(),
                    rvGoods.ResultDataSet.Tables[0].Rows[i]["MAX_WARN"].ToString().Trim() == "" ? "" : NumPriceFormat(rvGoods.ResultDataSet.Tables[0].Rows[i]["MAX_WARN"].ToString().Trim()),
                    rvGoods.ResultDataSet.Tables[0].Rows[i]["MIN_WARN"].ToString().Trim() == "" ? "" : NumPriceFormat(rvGoods.ResultDataSet.Tables[0].Rows[i]["MIN_WARN"].ToString().Trim()),
                    rvGoods.ResultDataSet.Tables[0].Rows[i]["MEMO"].ToString().Trim(),
                    rvGoods.ResultDataSet.Tables[0].Rows[i]["TRANS_STATE"].ToString().Trim());

                if (txtGOODS_ID.Text.Trim() == rvGoods.ResultDataSet.Tables[0].Rows[i]["GOODS_ID"].ToString().Trim())
                {
                    grdMain.Rows[0].Selected = false;
                    grdMain.Rows[i].Selected = true;
                    grdMain.FirstDisplayedScrollingRowIndex = 12 * (i / 12);
                }
            }
            grpSearchResult.Text = String.Format("检索结果  共{0}条", rvGoods.ResultDataSet.Tables[0].Rows.Count);
            SetGridBackColor();
        }

        /// <summary>	
        /// <p><b>方法名称</b>DateFormat</p>	
        /// <p><b>概要説明  :</b>date data format</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/08/27</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        /// <param name="strValue">没有格式化的字符串</param> 
        /// <returns type="string">格式化后的字符串</returns>
        protected string DateFormat( string strValue )
        {
            if (strValue.Length < 8)
            {
                return strValue;
            }
            return strValue.Substring(0, 4) + "/" +
                strValue.Substring(4, 2) + "/" +
                strValue.Substring(6, 2);
        }     

        /// <summary>	
        /// <p><b>方法名称</b>NumPriceFormat</p>	
        /// <p><b>概要説明  :</b>num and price data format</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/08/27</p>	
        /// <p><b>修改历史:</b></p>	
        /// </summary>
        /// <param name="strValue">没有格式化的字符串</param> 
        /// <returns type="string">格式化后的字符串</returns>
        protected string NumPriceFormat(string strValue)
        {
            string strTemp = strValue;            
            if (strValue.Length > 6)
            {
                strTemp = strValue.Substring(0, strValue.Length - 6) + ","
                    + strValue.Substring((strValue.Substring(0, strValue.Length - 6)).Length, 3)
                    + "," + strValue.Substring(strValue.Length - 3, 3);

            }
            else if (strValue.Length > 3)
            {
                strTemp = strValue.Substring(0, strValue.Length - 3) + ","
                + strValue.Substring((strValue.Substring(0, strValue.Length - 3)).Length, 3);
            }
            return strTemp;
        }

        /// <summary>
        /// 模式显示商品查询窗口
        /// </summary>
        /// <param name="SearchGoodsID">商品编号</param>
        /// <param name="strSearchGoodNo">商品条码</param>
        /// <param name="strSearchGoodNM">商品名称</param>
        public void ShowSearchGoodsDialog(Form fromOwner, string SearchGoodsID,string SearchGoodsNo,string SearchGoodsName)
        {
            IsSelect = true;
            txtGOODS_ID.Text = SearchGoodsID;
            txtGOODS_NO.Text = SearchGoodsNo;
            txtGOODS_NM1.Text = SearchGoodsName;
            this.Width = fromOwner.Width - 100;
            this.Height = fromOwner.Height - 100;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;

            if (SearchGoodsID.Length < 1 && SearchGoodsNo.Length < 1 && SearchGoodsName.Length < 1 && grdMain.Rows.Count > 0)
            {

            }
            else
            {
                BindGridData();
            }
            this.ShowDialog();
            IsSelect = false;
        }

        /// <summary>
        /// <p><b>方法名称</b>GoodsDeleteCheck</p>	
        /// <p><b>概要説明  :</b>检测该商品有没有没使用</p>
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/09/16</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="string" name="strGoodsID">商品编码</param>
        /// <returns type = "ReturnValue">商品编码</returns>
        public bool GoodsDeleteCheck(string strGoodsID)
        {
            ReturnValue rvReturnValue =
                bll_M007_GOODS.GoodsDeleteCheck(strGoodsID);

            if (rvReturnValue.IsSuccess == false)
            {
                Method.MessageShow(rvReturnValue.ErrorID,dataName);
                return false;
            }
            if (rvReturnValue.ResultDataSet.Tables[0].Rows.Count != 0)
            {
                Method.MessageShow("W11029", dataName);
                txtGOODS_ID.Focus();
                txtGOODS_ID.SelectAll();
                return false;
            }
            return true;
        }

        /// <summary>
        /// <p><b>方法名称</b>CheckName</p>
        /// <p><b>概要説明  :</b>添加时商品名的Check</p>
        /// <p><b>作成者    :</b>万岗</p>
        /// <p><b>作成日    :</b>2008/09/17</p>
        /// <p><b>修改历史  :</b></p>
        /// </summary>
        /// <returns type = "bool">false:存在 true:不存在</returns>
        public bool CheckNameAdd()
        {
            MODEL.MOD_M007_GOODS model = new XiaoGang.Soft.XGWMS.MODEL.MOD_M007_GOODS();
            model.GOODS_NM1 = txtGOODS_NM1.Text.Trim();
            ReturnValue rvReturnValue = bll_M007_GOODS.CheckName(model);
            if (rvReturnValue.IsSuccess == false)
            {
                Method.MessageShow(rvReturnValue.ErrorID,dataName);                
                return false;
            }
            if (0 != rvReturnValue.ResultDataSet.Tables[0].Rows.Count)
            {
                Method.MessageShow("W11E01", dataName);//本[@]已经存在。
                txtGOODS_NM1.Focus();
                return false;               
            }
            return true;
        }

        /// <summary>
        /// <p><b>方法名称</b>CheckName</p>	
        /// <p><b>概要説明  :</b>更新时商品名的Check</p>
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/09/17</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        /// <returns type = "bool">false:存在 true:不存在</returns>
        public bool CheckNameUpdate()
        {
            MODEL.MOD_M007_GOODS model =
                new XiaoGang.Soft.XGWMS.MODEL.MOD_M007_GOODS();
            model.GOODS_NM1 = txtGOODS_NM1.Text.Trim();
            model.GOODS_ID = txtGOODS_ID.Text.Trim();

            ReturnValue rvReturnValue = bll_M007_GOODS.CheckName(model);
            if (rvReturnValue.IsSuccess == false)
            {
                Method.MessageShow(rvReturnValue.ErrorID,dataName);
                return false;
            }
            if ((0 != rvReturnValue.ResultDataSet.Tables[0].Rows.Count) && model.GOODS_ID.Trim() != rvReturnValue.ResultDataSet.Tables[0].Rows[0]["GOODS_ID"].ToString().Trim())
            {
                Method.MessageShow("W11E01", dataName);//本[@]已经存在。
                txtGOODS_NM1.Focus();
                return false;
            }
            return true;
        }

        /// <summary>
        /// 选择商品
        /// </summary>
        private void SelectGoods()
        {
            if (grdMain.Rows.Count != 0)
            {
                GlobalVal.gloSelectedGoods.ClearItems();
                MODEL.MOD_M007_GOODS goods = new MODEL.MOD_M007_GOODS();
                goods.GOODS_ID = grdMain.CurrentRow.Cells["GOODS_ID"].Value.ToString().Trim();
                goods.GOODS_NM1 = grdMain.CurrentRow.Cells["GOODS_NM1"].Value.ToString().Trim();
                if (string.IsNullOrEmpty(grdMain.CurrentRow.Cells["MONEY"].Value.ToString().Trim()))
                {
                    goods.SalePrice = null;
                }
                else
                {
                    goods.SalePrice = decimal.Parse(grdMain.CurrentRow.Cells["MONEY"].Value.ToString().Trim());
                }
                if (grdMain.CurrentRow.Cells["Discount"].Value == null || string.IsNullOrEmpty(grdMain.CurrentRow.Cells["Discount"].Value.ToString().Trim()))
                {
                    goods.Discount = 1d;
                }
                else
                {
                    goods.Discount = double.Parse(grdMain.CurrentRow.Cells["Discount"].Value.ToString().Trim())/10;
                }
                GlobalVal.gloSelectedGoods.AddItem(goods);

                //if (FatherForm == "M009_STORAGE_OUTPUT")
                //{
                //    M009_STORAGE_OUTPUT.strGoodID = grdmentGoodsInfo.CurrentRow.Cells["GOODS_ID"].Value.ToString().Trim();
                //    M009_STORAGE_OUTPUT.strGoodNM = grdmentGoodsInfo.CurrentRow.Cells["GOODS_NM1"].Value.ToString().Trim();
                //    M009_STORAGE_OUTPUT.strSELLING_PRICE = grdmentGoodsInfo.CurrentRow.Cells["MONEY"].Value.ToString().Trim();
                //    M009_STORAGE_OUTPUT.strDiscount = grdmentGoodsInfo.CurrentRow.Cells["Discount"].Value.ToString().Trim();
                //}
                //else if (FatherForm == "M009_STORAGE_INPUT")
                //{
                //    M009_STORAGE_INPUT.strGoodID = grdmentGoodsInfo.CurrentRow.Cells["GOODS_ID"].Value.ToString().Trim();
                //    M009_STORAGE_INPUT.strGoodNM = grdmentGoodsInfo.CurrentRow.Cells["GOODS_NM1"].Value.ToString().Trim();
                //}
            }
            //this.Close();
            this.Hide();
        }

        /// <summary>
        /// 设置已删除商品的背景色
        /// </summary>
        private void SetGridBackColor ( )
        {
            Color bkColor2 = Color.DarkOrange;
            for (int i = 0; i < grdMain.Rows.Count; i++)
            {
                if ("3" == grdMain.Rows[i].Cells["TRANS_STATE"].Value.ToString().Trim())
                {
                    grdMain.Rows[i].DefaultCellStyle.BackColor = bkColor2;
                }
            }
        }

        /// <summary>
        /// 检测该商品有没有没使用
        /// </summary>
        /// <param name="strGoodsGroupID"></param>
        /// <returns>true:使用或发生错误 false:未使用</returns>
        protected bool IsGoodsUsed(string strGoodsID)
        {
            ReturnValue rvReturnValue = bll_M007_GOODS.GoodsDeleteCheck(strGoodsID);
            if (rvReturnValue.IsSuccess)
            {
                if (rvReturnValue.ResultDataSet.Tables[0].Rows.Count > 0)
                {
                    Method.MessageShow("W11D01", dataName);//本@正在使用!不能删除!
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                Method.MessageShow(rvReturnValue.ErrorID, dataName);
                return true;
            }
        }

        #endregion

        #region 事件

        private void M007_GOODS_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {                
                case Keys.F5:
                    AllButton_Click(btnAdd, e);
                    break;
                case Keys.Escape:
                    AllButton_Click(btnExit, e);
                    break;
            }          
        }

        private void GoodsForm_Load(object sender, EventArgs e)
        {
            if (!IsSelect)
            {
                BindGridData();
            }
            txtGOODS_ID.Focus();
        }

        private void grdmentGoodsInfo_SelectionChanged(object sender, EventArgs e)
        {
            if (!SearchFlag)
            {
                if (0 == grdMain.Rows.Count || grdMain.CurrentRow.Cells["GOODS_ID"].Value.ToString ().Trim () == "")
                {
                    return;
                }
                if (AddRowFlag)
                {
                    intFirstDisplayedScrollingRowIndex = grdMain.FirstDisplayedScrollingRowIndex;
                }
                //txtGOODS_ID.Text = grdmentGoodsInfo.CurrentRow.Cells["GOODS_ID"].Value.ToString ();
                //txtGOODS_NM1.Text = grdmentGoodsInfo.CurrentRow.Cells["GOODS_NM1"].Value.ToString ();
                //txtGOODS_NM2.Text = grdmentGoodsInfo.CurrentRow.Cells["GOODS_NM2"].Value.ToString ();
                //txtGOODS_NO.Text = grdmentGoodsInfo.CurrentRow.Cells["GOODS_NO"].Value.ToString ();
                //cmbGOODS_GROUP.SelectedValue = grdmentGoodsInfo.CurrentRow.Cells["GOODS_GROUP"].Value.ToString ().Trim ();
                //txtMAX_WARN.Text = grdmentGoodsInfo.CurrentRow.Cells["MAX_WARN"].Value.ToString ();
                //txtMEMO.Text = grdmentGoodsInfo.CurrentRow.Cells["MEMO"].Value.ToString ();
                //txtMIN_WARN.Text = grdmentGoodsInfo.CurrentRow.Cells["MIN_WARN"].Value.ToString ();
                //txtMoney.Text = grdmentGoodsInfo.CurrentRow.Cells["MONEY"].Value.ToString ();
                //txtDiscount.Text = grdmentGoodsInfo.CurrentRow.Cells["Discount"].Value.ToString ();
                //txtPROVIDER.Text = grdmentGoodsInfo.CurrentRow.Cells["PROVIDER"].Value.ToString ();
            }
        }

        private void grdmentGoodsInfo_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            if (IsSelect)
            {
                SelectGoods();
            }
            else
            {
                if (0 == grdMain.Rows.Count
                || e.RowIndex == -1
                || e.ColumnIndex == 10)
                {
                    return;
                }
                entityGoods.Clear();
                entityGoods.GOODS_ID = grdMain.CurrentRow.Cells["GOODS_ID"].Value.ToString();
                entityGoods.GOODS_NO = grdMain.CurrentRow.Cells["GOODS_NO"].Value.ToString();
                entityGoods.GOODS_NM1 = grdMain.CurrentRow.Cells["GOODS_NM1"].Value.ToString();
                entityGoods.GOODS_NM2 = grdMain.CurrentRow.Cells["GOODS_NM2"].Value.ToString();
                entityGoods.GOODS_GROUP = grdMain.CurrentRow.Cells["GOODS_GROUP"].Value.ToString();
                entityGoods.PROVIDER = grdMain.CurrentRow.Cells["PROVIDER"].Value.ToString();
                if (string.IsNullOrEmpty(grdMain.CurrentRow.Cells["MAX_WARN"].Value.ToString().Trim()))
                {
                    entityGoods.MAX_WARN = null;
                }
                else
                {
                    entityGoods.MAX_WARN = Int32.Parse(grdMain.CurrentRow.Cells["MAX_WARN"].Value.ToString());
                }
                if (string.IsNullOrEmpty(grdMain.CurrentRow.Cells["MIN_WARN"].Value.ToString().Trim()))
                {
                    entityGoods.MIN_WARN = null;
                }
                else
                {
                    entityGoods.MIN_WARN = Int32.Parse(grdMain.CurrentRow.Cells["MIN_WARN"].Value.ToString());
                }
                if (string.IsNullOrEmpty(grdMain.CurrentRow.Cells["MONEY"].Value.ToString().Trim()))
                {
                    entityGoods.SalePrice = null; 
                }
                else
                {
                    entityGoods.SalePrice = decimal.Parse(grdMain.CurrentRow.Cells["MONEY"].Value.ToString());
                }
                if (grdMain.CurrentRow.Cells["Discount"].Value==null || string.IsNullOrEmpty(grdMain.CurrentRow.Cells["Discount"].Value.ToString().Trim()))
                {
                    //entityGoods.Discount = 1;
                }
                else
                {
                    entityGoods.Discount = double.Parse(grdMain.CurrentRow.Cells["Discount"].Value.ToString())/10;
                }
                entityGoods.MEMO = grdMain.CurrentRow.Cells["MEMO"].Value.ToString();
                if (new M007GoodsEdit(entityGoods).ShowDialog() == DialogResult.OK)
                {
                    //刷新Grid数据
                    BindGridData();
                }
            }
        }

        /// <summary>
        /// 单击删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grdmentGoodsInfo_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            int intColumnIndex = e.ColumnIndex;
            //删除
            if (intColumnIndex == 10 && e.RowIndex != -1)
            {
                //物理彻底删除
                if (grdMain.CurrentRow.Cells["TRANS_STATE"].Value.ToString() == "3")
                {
                    this.Cursor = Cursors.WaitCursor;
                    //检查该分类是否正在被使用
                    if (IsGoodsUsed(grdMain.CurrentRow.Cells["GOODS_ID"].Value.ToString()))
                    {
                        this.Cursor = Cursors.Default;
                        return;
                    }
                    if (Method.MessageShow("Q22017", dataName) == DialogResult.Yes)//本条数据已被逻辑删除。确定要进行彻底删除吗?
                    {
                        ReturnValue result = bll_M007_GOODS.DeleteProcessPhysical(grdMain.CurrentRow.Cells["GOODS_ID"].Value.ToString());
                        if (result.IsSuccess)
                        {
                            BindGridData();
                        }
                        else
                        {
                            Method.MessageShow(result.ErrorID, dataName);
                        }
                    }
                    this.Cursor = Cursors.Default;
                    return;
                }
                //逻辑删除
                if (Method.MessageShow("Q22007", dataName) == DialogResult.Yes)//确定要删除该条数据吗?
                {
                    this.Cursor = Cursors.WaitCursor;
                    try
                    {
                        entityGoods.UPDATER_ID = GlobalVal.gloUserInfo.USER_ID;
                        entityGoods.TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD;
                        entityGoods.GOODS_ID = grdMain.CurrentRow.Cells["GOODS_ID"].Value.ToString();
                        ReturnValue result = bll_M007_GOODS.DeleteProcessLogic(entityGoods);
                        if (result.IsSuccess)
                        {
                            BindGridData();
                        }
                        else
                        {
                            Method.MessageShow(result.ErrorID, dataName);
                        }
                    }
                    catch
                    {
                        Method.MessageShow("E11004", dataName);//An unknown error process!
                    }
                    finally
                    {
                        this.Cursor = Cursors.Default;
                    }
                }
            }
        }
        
        private void AllButton_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            SearchFlag = true;
            switch (((Button)sender).Name)
            {
                #region btnAdd
                case "btnAdd":
                    {
                        if (new M007GoodsEdit().ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            BindGridData();
                        }
                    }
                    break;
                #endregion    

                #region btnExit
                case "btnExit":
                    if (IsSelect)
                    {
                        this.Hide();
                    }
                    else
                    {
                        this.Close();
                    }
                    break;
                #endregion                

                #region btnNoManage
                case "btnNoManage":
                    {
                        if (txtGOODS_ID.Text.Trim().Length>0)
                        {
                            MODEL.MOD_M007_GOODS mod = new XiaoGang.Soft.XGWMS.MODEL.MOD_M007_GOODS ();
                            mod.GOODS_ID = txtGOODS_ID.Text.Trim ();
                            ReturnValue reValue = bll_M007_GOODS.GetSingleData (mod);
                            if(reValue.ResultDataSet.Tables[0].Rows.Count<1)
                            {
                                Method.MessageShow ("W11053",dataName);//请选择商品或输入有效的商品编号！
                                this.Cursor = Cursors.Default;
                                return;
                            }
                            FrmNoManage frm = new FrmNoManage (txtGOODS_ID.Text.Trim ());
                            frm.ShowDialog ();
                        }  
                    }
                    break;
                #endregion

            }
            this.Cursor = Cursors.Default;
            SearchFlag = false;
        }

        protected override void AllTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            base.AllTextBox_KeyDown(sender, e);
        }

        private void AllTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            string strControlName = ((TextBox)sender).Name;
            switch (strControlName)
            {
                case "txtGOODS_ID":
                    FormBase.UKeyPress(e, InputType.Num);
                    break;
                case "txtGOODS_NO":
                    FormBase.UKeyPress(e, InputType.Num);
                    break;
                case "txtMAX_WARN":               
                    FormBase.UKeyPress(e, InputType.Num);
                    break;
                case "txtMIN_WARN":
                    FormBase.UKeyPress(e, InputType.Num);
                    break;
                case "txtMoney":
                    FormBase.UKeyPress(e, InputType.Dbl);
                    break;
                case "txtDiscount":
                    FormBase.UKeyPress(e, InputType.Dbl);
                    break;
                case "txtUNITS":
                    FormBase.UKeyPress(e, InputType.Num);
                    break;
                case "txtBUYING_PRICE":
                    FormBase.UKeyPress(e, InputType.Dbl);
                    break;
            }
        }

        private void AllTextBox_GotFocus(object sender, EventArgs e)
        {
            string strValue = ((TextBox)sender).Text.Trim();
            switch (((TextBox)sender).Name)
            {
                case "txtGOODS_ID":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Disable;
                    break;
                case "txtGOODS_NM1":                   
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Hiragana;
                    break;
                case "txtGOODS_NM2":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Hiragana;
                    break;
                case "txtGOODS_NO":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Disable;
                    break;
                case "txtMAX_WARN":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Disable;
                    ((TextBox)sender).Text = strValue.Replace(",", "");
                    break;
                case "txtMEMO":                    
                    ((TextBox)sender).ImeMode = ImeMode.Hiragana;
                    break;
                case "txtMIN_WARN":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Disable;
                    ((TextBox)sender).Text = strValue.Replace(",", "");
                    break;
                case "txtMoney":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Disable;
                    ((TextBox)sender).Text = strValue.Replace(",", "");
                    break;
                case "txtPROVIDER":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Hiragana;
                    break;
            }
        }

        private void AllTextBox_LostFocus(object sender, EventArgs e)
        {
            string strValue = ((TextBox)sender).Text.Trim().Replace("/", "");
            strValue = strValue.Replace(",", "");
            if (0 == strValue.Length)
            {
                return;
            }

            switch (((TextBox)sender).Name)
            {
                case "txtMAX_WARN":
                    ((TextBox)sender).Text = NumPriceFormat(strValue);
                    break;
                case "txtMIN_WARN":
                    ((TextBox)sender).Text = NumPriceFormat(strValue);
                    break;
                case "txtMoney":
                    ((TextBox)sender).Text = NumPriceFormat(strValue);
                    break;
            }
        }

        private void M007_GOODS_Activated(object sender, EventArgs e)
        {
            GlobalVal.gloSelectedGoods.ClearItems();
            //if (!blFormLoad)
            //{
            //    string strValueGoodsGroup = cmbGOODS_GROUP.SelectedValue.ToString();
            //    Method.CmbDataBound("GOODS_GROUP_ID", cmbGOODS_GROUP);
            //    foreach (System.Data.DataRowView drGoods in this.cmbGOODS_GROUP.Items)
            //    {
            //        if (strValueGoodsGroup == drGoods["GOODS_GROUP_ID"].ToString().Trim())
            //        {
            //            cmbGOODS_GROUP.SelectedValue = strValueGoodsGroup;
            //            return;
            //        }
            //    }
            //    cmbGOODS_GROUP.SelectedIndex = 0;
            //}
            //blFormLoad = false;
        }

        private void btnGOODS_GROUP_Click(object sender, EventArgs e)
        {
            M006_GOODS_GROUP form = new M006_GOODS_GROUP(true);
            form.ShowDialog();
            InitialComBox();
        }

        private void grdmentGoodsInfo_KeyDown(object sender, KeyEventArgs e)
        {
            if (IsSelect)
            {
                if (e.KeyValue == 13)
                {
                    SelectGoods();
                }
            }
        }        

        public  void M007_GOODS_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString().Trim() == "")
            {
                txtGOODS_NO.Text = "";
                txtGOODS_NO.Focus();
            }
        }

        /// <summary>
        /// 查询条件改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QueryChanged(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            BindGridData();
        }

        #endregion

    }
}